package com.hmdp.httpclienTest;



import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author shifengqiang 2022/3/15 6:03 PM
 */
public class HttpClientT4 {

    //设置请求访问的ssl证书
    private static SSLContext ctx;

    static {
        try {
            ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] arg0,
                                               String arg1) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] arg0,
                                               String arg1) throws CertificateException {
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
        } catch (Exception e) {
            System.out.println(e);
        }
    }



    public static void main(String[] args) throws Exception {
        String url="https://taolitop.com";
/*        String url = "https://www.tuicool.com";
        String html = HttpClientT4.resultHtml(url);
        System.out.println(html);*/

       List<String> strings = HttpClientT4.resultImgUrl(url);
        for (String imgUrl : strings) {
            System.out.println(imgUrl);
        }

    }


    /**
     * 返回Html
     * @param url
     * @return
     * @throws Exception
     */
    public static String resultHtml (String url) throws Exception{
        // 在这儿加了 setSSLContext(ctx)
        String result = "爬取错误！";
        CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(ctx).build();

        //创建get请求
        HttpGet request = new HttpGet(url);
/*        request.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
        //如果你伪装了之后，如果短时间内一直多次访问的话，网站会对你的ip进行封杀，这个时候就需要换个ip地址了，使用代理IP
        HttpHost proxy = new HttpHost("112.85.168.223", 9999);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        request.setConfig(config);*/

        try {
            HttpResponse response = httpClient.execute(request);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            //System.out.println(result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 返回图片url
     * @param url
     * @return
     * @throws Exception
     */
    public static List<String> resultImgUrl (String url) throws Exception{
        // 在这儿加了 setSSLContext(ctx)
        String result = "爬取错误！";
        List<String> imgList=new ArrayList<>();
        CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(ctx).build();

        //创建get请求
        HttpGet request = new HttpGet(url);

        try {
            HttpResponse response = httpClient.execute(request);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            Document parse = Jsoup.parse(result);
            Elements links = parse.getElementsByTag("img");
            for (Element link : links) {
                String linkHref = link.attr("src");
                linkHref = "https://taolitop.com" + linkHref;
                imgList.add(linkHref);
                //System.out.println(linkHref);
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgList;
    }

}

